﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;


namespace gpao2012
{
    public partial class infosoc : DevExpress.XtraEditors.XtraUserControl
    {
        public infosoc()
        {
            InitializeComponent();

        }

        private void annuler_Click(object sender, EventArgs e)
        {

        }

        private void enregistrer_Click(object sender, EventArgs e)
        {


            if (raisonsocial.Text == "")
            { raisonsocial.BackColor = Color.LightPink; }

            if (adresse.Text == "")
            { adresse.BackColor = Color.LightPink; }

            if (ville.Text == "")
            { ville.BackColor = Color.LightPink; }

            if (tel.Text == "")
            { tel.BackColor = Color.LightPink; }

            if (url.Text == "")
            { url.BackColor = Color.LightPink; }

            if (prixminute.Text == "")
            { prixminute.BackColor = Color.LightPink; }

            if (prixicppp.Text == "")
            { prixicppp.BackColor = Color.LightPink; }

            if (pays.Text == "")
            { pays.BackColor = Color.LightPink; }

            if (fax.Text == "")
            { fax.BackColor = Color.LightPink; }

            if (mail.Text == "")
            { mail.BackColor = Color.LightPink; }

            if (codepostal.Text == "")
            { codepostal.BackColor = Color.LightPink; }


            if ((raisonsocial.Text != "") || (adresse.Text != "") || (ville.Text != "") || (tel.Text != "") || (url.Text != "") || (prixminute.Text != "") || (codepostal.Text != "") || (prixicppp.Text != "") || (pays.Text != "") || (fax.Text != "") || (mail.Text != ""))
            {
                SqlConnection con;
                SqlCommand cmd;
                con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=gpaogscofficiel;Integrated Security=True");
                String qraisonsocial = raisonsocial.Text;
                String qadress = adresse.Text;
                String qville = ville.Text;
                String qpays = pays.Text;
                String qtel = tel.Text;
                String qfax = fax.Text;
                String qcodepostal = codepostal.Text;
                String qmail = mail.Text;
                int qprixminute = Convert.ToInt32(prixminute.Text);
                int qprixicppp = Convert.ToInt32(prixicppp.Text);
                String qurl = url.Text;
                //Image qlogo = logo.Image.; , [logo]  , "+qlogo+"
                string sqlquery = "UPDATE [gpaogscofficiel].[dbo].[info_soc] SET raison_social = '" + qraisonsocial + "' ,[adress] ='" + qadress +
          "' ,[ville] ='" + qville +
          "' ,[pays] = '" + qpays +
          "' ,[tel] = '" + qtel +
          "' ,[fax] = '" + qfax +
          "' ,[codepostal] = '" + qcodepostal +
          "' ,[mail] = '" + qmail +
          "' ,[url] = '" + qurl +
          "' ,[prixminute] =" + qprixicppp +
          " ,[prixicppp] = " + qprixminute;

                con.Open();
                cmd = new SqlCommand(sqlquery, con);
                /*   ,[logo] = <logo, image,>
                         string sqlquery = "INSERT INTO [dbo].[info_soc] ([raison_social], [adress], [ville], [pays], [tel], [fax], [codepostal], [mail], [url], [prixminute], [prixicppp]) VALUES ('"+qraisonsocial+"', '"+qadress+"', '"+qville+"', '"+qpays+"', '"+qtel+"', '"+qfax+"', '"+qcodepostal+"', '"+qmail+"', '"+qurl+"', "+qprixminute+", "+qprixicppp+")";
                         con.Open();
                            cmd = new SqlCommand("DELETE FROM [gpaogscofficiel].[dbo].[info_soc]",con);
                            cmd.ExecuteNonQuery();
                            cmd = new SqlCommand(sqlquery,con);
                         */
                cmd.ExecuteNonQuery();
                statustext.Text = "Sauvgarde effectué !";
                con.Close();

            }
        }

        private void ouvririmage_Click(object sender, EventArgs e)
        {
        //    logo.LoadImage();
        }

        private void infosoc_Load(object sender, EventArgs e)
        {

            SqlConnection con;
            SqlCommand cmd;
            con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=gpaogscofficiel;Integrated Security=True");
            string sqlquery = "SELECT * FROM [gpaogscofficiel].[dbo].[info_soc]";
            con.Open();
            // Innitialisé la base (si la table et vide)

            /*   cmd = new SqlCommand(sqlquery, con);
                 SqlDataReader mySqDataReader = cmd.ExecuteReader();
                 mySqDataReader.Read();
                 MessageBox.Show( (string) mySqDataReader["id_inf_soc"]);
                 if ( (mySqDataReader["id_inf_soc"] == System.DBNull.Value))
                 {
                     MessageBox.Show("haha");
                     string sql = "INSERT INTO [gpaogscofficiel].[dbo].[info_soc] ([raison_social],[adress],[ville],[pays],[tel],[fax],[codepostal],[mail],[url],[prixminute],[prixicppp],[logo]) VALUES ('','','','','','','','','',0,0,null)";
                     cmd = new SqlCommand(sql, con);
                     cmd.ExecuteNonQuery();
                 }
                 */
            // importation de donné 
            cmd = new SqlCommand(sqlquery, con);
            SqlDataReader mySqDataReader = cmd.ExecuteReader();
            if (mySqDataReader.Read())
            {
                if (mySqDataReader["id_inf_soc"] != System.DBNull.Value)
                {
                    this.raisonsocial.Text = (string)mySqDataReader["raison_social"];
                    this.adresse.Text = (string)mySqDataReader["adress"];
                    this.ville.Text = (string)mySqDataReader["ville"];
                    this.pays.Text = (string)mySqDataReader["pays"];
                    this.tel.Text = (string)mySqDataReader["tel"];
                    this.fax.Text = (string)mySqDataReader["fax"];
                    this.codepostal.Text = (string)mySqDataReader["codepostal"];
                    this.mail.Text = (string)mySqDataReader["mail"];
                    this.url.Text = (string)mySqDataReader["url"];
                    this.prixminute.Text = mySqDataReader.GetInt32(10).ToString("G");
                    this.prixicppp.Text = mySqDataReader.GetInt32(11).ToString("G");

                }
            }
        }

        private void panelControl1_Paint(object sender, PaintEventArgs e)
        {

        }



    }
}